<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
      <el-form-item label="运单号" prop="code">
        <el-input
          v-model="queryParams.code"
          clearable
          placeholder="请输入运单号"
          @keyup.enter.native="handleShipmentQuery"
        />
      </el-form-item>
      <el-form-item label="运单状态" prop="status">
        <el-select v-model="queryParams.status" clearable filterable placeholder="请选择运单状态">
          <el-option v-for="dict in dict.type.OrderStatus" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="license">
        <el-input
          v-model="queryParams.license"
          clearable
          placeholder="请输入车牌号"
          @keyup.enter.native="handleShipmentQuery"
        />
      </el-form-item>
      <el-form-item label="运作模式" prop="operationPattern">
        <el-select v-model="queryParams.operationPattern" clearable placeholder="请选择运作模式">
          <el-option
            v-for="dict in dict.type.OperationPattern"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="司机名称" prop="outDriver">
        <el-input
          v-model="queryParams.outDriver"
          clearable
          placeholder="请输入司机名称"
          @keyup.enter.native="handleShipmentQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleShipmentQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetShipmentQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button :disabled="single" icon="el-icon-location" plain size="mini" type="primary" @click="handleOpenTrack">
          查看在途信息
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="shipmentList" @selection-change="handleSelectionChange" @row-dblclick="handleOpenTrack">
      <el-table-column align="center" fixed type="selection" width="55"/>
      <el-table-column align="center" fixed label="组织机构" prop="dtMap.jcOrganizationIdDtLabel"/>
      <el-table-column align="center" fixed label="运单号" prop="code" width="130"/>
      <el-table-column align="center" fixed label="运单日期" prop="time" width="180"/>
      <el-table-column align="center" fixed label="运单状态" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.OrderStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="车牌号" prop="license" width="100"/>
      <el-table-column align="center" label="起运站" prop="dtMap.jcFromOrganizationIdDtLabel"/>
      <el-table-column align="center" label="当前站" prop="dtMap.jcNowOrganizationIdDtLabel"/>
      <el-table-column align="center" label="下一站" prop="dtMap.jcNextOrganizationIdDtLabel"/>
      <el-table-column align="center" label="目的站" prop="dtMap.jcToOrganizationIdDtLabel"/>
      <el-table-column align="center" label="途径站" prop="orgnameList"/>
      <el-table-column align="center" label="总金额" prop="totalFee"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="订单号" prop="orderCode" width="200"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="客户单号" prop="relateBill1" width="200"/>
      <el-table-column align="center" label="运作模式" prop="operationPattern">
        <template #default="scope">
          <dict-tag :options="dict.type.OperationPattern" :value="scope.row.operationPattern"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="承运商类型" prop="carrierType">
        <template #default="scope">
          <dict-tag :options="dict.type.Cytp" :value="scope.row.carrierType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="承运商" prop="dtMap.jcCarrierIdDtLabel"/>
      <el-table-column align="center" label="承运商是否超期" prop="carriageIsExceed">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.carriageIsExceed"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数量" prop="number"/>
      <el-table-column align="center" label="货值" prop="value"/>
      <el-table-column align="center" label="体积" prop="volume"/>
      <el-table-column align="center" label="重量" prop="weight"/>
      <el-table-column align="center" label="创建人" prop="createName"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="100"/>
      <el-table-column align="center" label="修改人" prop="modifyName"/>
      <el-table-column align="center" label="修改时间" prop="modifyTime" width="180"/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getShipmentList"
    />
    <!-- 运单追踪内容 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="900px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="事件" prop="event">
              <el-input v-model="form.event" placeholder="请输入内容" rows="5" type="textarea"/>
            </el-form-item>
            <el-form-item label="备注" prop="description">
              <el-input v-model="form.description" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="照片" prop="picture">
              <image-upload v-model="form.picture"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row :gutter="10" class="mb8" style="text-align: center;">
        <el-button v-hasPermi="['shipment:track:add']" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </el-row>
      <el-table v-loading="loading" :data="trackList" style="max-height: 400px; overflow-y: scroll;">
        <el-table-column align="center" label="序号" type="index" width="55"/>
        <el-table-column align="center" label="操作人" prop="person"/>
        <el-table-column align="center" label="追踪时间" prop="time" width="180"/>
        <el-table-column align="left" label="事件" prop="event" width="300"/>
        <el-table-column align="center" label="照片" prop="picture">
          <template #default="scope">
            <el-image :src="imageUrls(scope.row.picture)" fit="cover" style="width: 100px; height: 100px"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" prop="description"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {trackShipmentList} from "@/api/shipment/shipment";
import {listTrack, addTrack} from "@/api/shipment/track";

export default {
  name: "ShipmentTrack",
  dicts: ['CommDataStatus', 'Cytp', 'CommIsNot', 'OrderStatus', 'OperationPattern'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运单追踪表格数据
      trackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        backNumber: null,
        carriageIsExceed: null,
        carrierType: null,
        factArriveTime: null,
        factLeaverTime: null,
        feeType: null,
        isAbnormal: null,
        license: null,
        number: null,
        operationPattern: null,
        orderCode: null,
        outDriver: null,
        planLeaveTime: null,
        refuse: null,
        relateBill1: null,
        requireArriveTime: null,
        shipmentMethod: null,
        tan: null,
        time: null,
        value: null,
        volume: null,
        weight: null,
        jcCarrierId: null,
        jcDriverId: null,
        jcOrganizationId: null,
        jcOrganizationIdDtLabel: null,
        jcSettlementId: null,
        jcVehicleId: null,
        jcVehicleHeadId: null,
        actionTime: null,
        jcFromOrganizationId: null,
        jcFromOrganizationIdDtLabel: null,
        jcNowOrganizationId: null,
        jcNowOrganizationIdDtLabel: null,
        jcNextOrganizationId: null,
        jcNextOrganizationIdDtLabel: null,
        jcToOrganizationId: null,
        jcToOrganizationIdDtLabel: null,
        orgIds: null,
        outPhone: null,
        orgnameList: null,
        jzWeight: null,
        isConfirm: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        event: [{required: true, message: "请输入事件", trigger: "blur"}]
      },
      shipmentList: [],
      shipmentId: null,
      openPicList: []
    };
  },
  created() {
    this.getShipmentList();
  },
  methods: {
    imageUrls(picture) {
      // this.openPicList = picture.split(',').map(d => process.env.VUE_APP_BASE_API + d);
      return picture ? process.env.VUE_APP_BASE_API + picture : require("@/assets/images/not-exist.jpg");
    },
    /** 查询新运单管理列表 */
    getShipmentList() {
      this.loading = true;
      this.queryParams.params = {};
      trackShipmentList(this.queryParams).then(response => {
        this.shipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleShipmentQuery() {
      this.queryParams.pageNum = 1;
      this.getShipmentList();
    },
    /** 重置按钮操作 */
    resetShipmentQuery() {
      this.resetForm("queryForm");
      this.getShipmentList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查询运单追踪列表 */
    getList() {
      this.loading = true;
      listTrack(this.shipmentId).then(response => {
        this.trackList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        event: null,
        person: null,
        time: null,
        type: null,
        jcShipmentId: null,
        picture: null,
        coordinates: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAddTrack() {
      this.reset();
    },
    /** 修改按钮操作 */
    handleOpenTrack(row) {
      this.open = true;
      this.reset();
      this.shipmentId = row.id || this.ids
      this.loading = true;
      listTrack(this.shipmentId).then(response => {
        this.loading = false;
        this.trackList = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.form.jcShipmentId = this.shipmentId;
          addTrack(this.form).then(response => {
            this.loading = false;
            this.$modal.msgSuccess("新增成功");
            this.getList();
          });
        }
      });
    }
  }
};
</script>
